package haw.its;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.Scanner;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

public class SMTPMail {

	private static void sendMail2(String host, String to, String realFrom,
			String from, String subject, String msg, final String user,
			final String pw) {

		Properties properties = System.getProperties();
		// Setup mail server
		properties.put("mail.smtp.auth", "true");
		properties.setProperty("mail.smtp.host", host);
		properties.put("mail.smtp.port", "587");
		properties.put("mail.smtp.starttls.enable", "true");

		// authenticate
		Session session = Session.getDefaultInstance(properties,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(user, pw);
					}
				});

		// Create a MimeMessage.
		MimeMessage message = new MimeMessage(session);

		Address addressReal;
		Address address;
		try {
			addressReal = new InternetAddress(realFrom);
			message.setFrom(addressReal);

			address = new InternetAddress(from);
			Address[] adrs = new Address[1];
			adrs[0] = address;

			message.setReplyTo(adrs);

			// message.setSender(address);

			message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to));

			message.setSubject(subject);
			message.setText(msg);

			Transport.send(message);

			System.out.println("Send");
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		System.out.print("Send to?: ");
		String to = scan.nextLine();

		System.out.print("Fake Send as?: ");
		String fakeFrom = scan.nextLine();

		System.out.print("Username?: ");
		String user = scan.nextLine();

		JPasswordField pwd = new JPasswordField(20);
		JOptionPane.showConfirmDialog(null, pwd, "Enter Password",
				JOptionPane.OK_CANCEL_OPTION);

		String pw = new String(pwd.getPassword());

		scan.close();

		sendMail2("haw-mailer.haw-hamburg.de", to, fakeFrom,
				"Donald.Duck@Disneyland.de", "Test", "Hallo", user, pw);
	}

	public static void sendMail(String host, String to, String realFrom,
			String from, String subject, String msg) {

		String domain = from.substring(from.indexOf('@') + 1);

		Socket s;
		try {
			s = new Socket(host, 587);

			String rn = "\r\n";
			PrintStream out = new PrintStream(s.getOutputStream());
			BufferedReader in = new BufferedReader(new InputStreamReader(
					s.getInputStream()));

			out.print("HELO " + domain + rn);
			System.out.println(in.readLine());
			out.print("MAIL FROM: <" + realFrom + ">" + rn);
			System.out.println(in.readLine());
			out.print("RCPT TO: <" + to + ">" + rn);
			System.out.println(in.readLine());
			out.print("DATA" + rn);
			System.out.println(in.readLine());
			out.print("From: <" + from + ">\r\n");
			System.out.println(in.readLine());
			out.print("To: <" + to + ">\r\n");
			System.out.println(in.readLine());
			out.print("Subject: " + subject + "\r\n");
			System.out.println(in.readLine());
			out.print(msg + "\r\n");
			System.out.println(in.readLine());
			out.print(".\r\n");
			System.out.println(in.readLine());

			out.print("QUIT");
			System.out.println(in.readLine());
			s.close();

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
